package com.housekeeping.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class ApiResponse<T> {
    
    private boolean success;
    private String message;
    private T data;
    private int code;
    private long timestamp;

    public ApiResponse() {
        this.timestamp = System.currentTimeMillis();
    }

    public ApiResponse(boolean success, String message, T data, int code) {
        this.success = success;
        this.message = message;
        this.data = data;
        this.code = code;
        this.timestamp = System.currentTimeMillis();
    }


    public static <T> ApiResponse<T> success(T data) {
        return new ApiResponse<>(true, "操作成功", data, 200);
    }

    public static <T> ApiResponse<T> success(String message, T data) {
        return new ApiResponse<>(true, message, data, 200);
    }

    public static ApiResponse<Object> success() {
        return new ApiResponse<>(true, "操作成功", null, 200);
    }


    public static ApiResponse<Object> error(String message) {
        return new ApiResponse<>(false, message, null, 500);
    }

    public static ApiResponse<Object> error(int code, String message) {
        return new ApiResponse<>(false, message, null, code);
    }


    public static ApiResponse<Object> badRequest(String message) {
        return new ApiResponse<>(false, message, null, 400);
    }


    public static ApiResponse<Object> unauthorized(String message) {
        return new ApiResponse<>(false, message, null, 401);
    }


    public static ApiResponse<Object> forbidden(String message) {
        return new ApiResponse<>(false, message, null, 403);
    }


    public static ApiResponse<Object> notFound(String message) {
        return new ApiResponse<>(false, message, null, 404);
    }

    public static void sendJsonResponse(javax.servlet.http.HttpServletRequest request, HttpServletResponse response, ApiResponse<?> apiResponse) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        response.setStatus(apiResponse.getCode());


        String origin = request.getHeader("Origin");
        if (origin != null && (origin.equals("http://localhost:3000") || origin.equals("http://127.0.0.1:3000"))) {
            response.setHeader("Access-Control-Allow-Origin", origin);
            response.setHeader("Vary", "Origin");
            response.setHeader("Access-Control-Allow-Credentials", "true");
        }
        response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type, X-Requested-With, Accept, Origin");

        ObjectMapper objectMapper = new ObjectMapper();
        PrintWriter out = response.getWriter();
        out.print(objectMapper.writeValueAsString(apiResponse));
        out.flush();
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }
} 